package org.example.demo.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.demo.constant.Constants;
import org.example.demo.enums.ResultStatus;
import org.example.demo.model.*;
import org.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired  //告诉Spring 从容器中取出这个对象,作为给当前对象(BookController)的属性
    private BookService bookService;



    //添加图书
    @RequestMapping(value="/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        //校验参数
        log.info("添加图书,接收到参数book Info: {}",bookInfo);

        if(!StringUtils.hasLength(bookInfo.getBookName())
                ||!StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() ==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() ==null) {
            return "参数不合法";
        }
        //添加图书
        try{
            Integer result = bookService.insertBook(bookInfo);
            if(result > 0) {
                return "";
            }
        }catch(Exception e){
            log.error("添加图书异常,e:",e);
        }

        return "添加失败";
    }

    //查询图书列表
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询图书列表,请求参数pageRequest: {}",pageRequest);

        //从session中获取用户信息
        //如果用户信息为空,说明用户未登录,就跳转到登录界面
//        UserInfo loginUserInfo =(UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);  //通过session得到用户登录的信息
//        if(loginUserInfo == null  || loginUserInfo.getId() < 0) {  //用户不存在
//            return Result.nologin();
//        }


        PageResult<BookInfo> bookList= bookService.getBookListByPage(pageRequest);  //把参数传到 service 层再转到数据层执行SQL语句
        return Result.success(bookList);  //返回给前端
    }

    //修改图书
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("根据ID查询图书信息, id: "+bookId);
        //打印方法耗时
        long start = System.currentTimeMillis();
        BookInfo bookInfo = bookService.queryBookById(bookId);
        long end = System.currentTimeMillis();
        log.info("queryBookById 耗时: "+(end-start)+"ms");

        return bookService.queryBookById(bookId);
    }

    //更新图书
    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书,bookInfo: {}"+bookInfo);
        try{
            Integer result = bookService.updateBookById(bookInfo);
            if (result > 0) {
                return "";
            }
            return "内部错误,请联系管理员";
        }catch (Exception e){
            log.error("更新图书失败,e:",e);
            return "更新图书失败: " + e.getMessage();
        }

    }

    //删除图书
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("删除图书,bookInfo: {}"+bookId);
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(0);
            Integer result = bookService.updateBookById(bookInfo);

            if (result > 0) {
                return "";
            }
            return "内部错误,请联系管理员";
        }catch (Exception e){
            log.error("更新图书失败,e:",e);
            return "更新图书失败: " + e.getMessage();
        }
    }

    //批量删除图书
    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids: {}", ids);
        Integer result = bookService.batchDeleteBookByIds(ids);
        if(result > 0) {
            return "";
        }
        return "删除失败,联系管理员";
    }
}
